'''============================
@Time : 2023/12/12 11:00
@Author : YangLiHua
@File : test_data.py
@Desc : 读写文件的整合
============================'''
from config.config import file_path
class ReadWrite:
    def __init__(self):
        # self.txtpath=r'D:\develop\repos3\Python_basic\A_python\test_data\test.txt'
        # self.excelpath=r'D:\develop\repos3\Python_basic\B_web_selenium\selenium_day4\test_data\a.xlsx'
        # self.yamlpath=r'D:\develop\repos3\Python_basic\A_python\test_data\test.yml'
        self.txtpath = ""
        self.excelpath = file_path
        self.yamlpath = ""

    def txtread(self):
        list1 = []
        # 打开文件，读取文件内容
        with open(self.txtpath, 'r', encoding='utf-8') as f:
            values = f.readlines()
            f.close()
        for data in values:
            data_v = data.strip('\n')
            list1.append(data_v)
        return list1

    def txtwrite(self,username,password):
        f = open(self.txtpath, 'a', encoding='utf-8')
        values = f'{username},{password}\n'
        f.writelines(values)
        f.close()

    def excelread(self,sheetname):
        import openpyxl
        # 获取工作簿
        wb=openpyxl.load_workbook(self.excelpath)
        # 获取工作表
        table = wb[sheetname]
        # 获取最大行
        rows = table.max_row
        # 获取最大列
        cols = table.max_column
        # 设置一个列表用于存放所有的列
        list2 = []
        # 循环读取每一个单元格
        for row in range(2,rows+1):
            # 设置一个列表用于存放每一行的数据
            list1 = []
            for col in range(1,cols+1):
                values=table.cell(row,col).value
                list1.append(values)
            list2.append(list1)
        wb.close()
        return list2

    def excelwrite(self,*args,sheetname):
        import openpyxl
        # 获取工作簿
        wb = openpyxl.load_workbook(self.excelpath)
        # 获取工作表
        table = wb[sheetname]
        # 获取最大行
        rows = table.max_row
        # 获取最大列
        cols = len(args)
        # 将数据添加到工作表中
        for col in range(cols):
            table.cell(rows+1,col+1).value = col
        # 保存
        wb.save(self.excelpath)

    def ymlread(self):
        import yaml
        # 打开文件
        f = open(yamlpath, 'r', encoding='utf-8')
        # 文件的读取
        content = f.read()
        # yml文件的读取(拿到的数据是字典)
        data = yaml.safe_load(content)
        # 关闭文件
        f.close()
        return data

    def ymlwrite(self,username,password):
        import yaml
        # 打开文件
        f = open(yamlpath, 'r', encoding='utf-8')
        data={'username':username,'password':password}
        # yml文件的写入
        yaml.safe_dump(data1, f)
        # 关闭文件
        f.close()

    def mysqlread(self):
        import pymysql
        # 链接数据库
        db=pymysql.connect(host='localhost',port=3306,user='root',password='123456',database='task1203',charset='utf8')
        # 创建游标
        cur = db.cursor()
        # sql语句
        sql = 'select * from users where username="test001"'
        # 进行查询
        cur.execute(sql)

    def mysqlwrite(self,username,password):
        import pymysql
        # 链接数据库
        db = pymysql.connect(host='localhost', port=3306, user='root', password='123456', database='task1203',
                             charset='utf8')
        # 创建游标
        cur = db.cursor()
        # sql语句
        sql = f'insert into users values("{username}","{password}")'
        # 进行
        cur.execute(sql)
        # 提交到数据库
        db.commit()

# if __name__=='__main__':
#     users = ReadWrite().excelread('login')
#     print(users)